﻿using System;
using System.IO;
using System.Windows.Forms;
using Xin478.CSWork.Common;
using NBearLite;

namespace Xin478.CSWork.Client
{
    public class UserSession
    {
        private static readonly string AutoLoginInfoPath = Application.StartupPath + "\\Users\\p.dat";
        const string LOCALPASSWORDKEY = "XIN478'S APPLICATION 2009-11-13";

        private static UserSession instance;

        /// <summary>
        /// 返回当前用户的相关信息
        /// </summary>
        public static UserSession Current
        {
            get { return instance; }
        }

        /// <summary>
        /// 重置当前用户信息，并填充新用户信息
        /// </summary>
        /// <param name="employeeInfo"></param>
        public static void NewSession(EmployeeInfo employeeInfo)
        {
            if (employeeInfo == null) return;

            instance = new UserSession();
            instance.EmployeeInfo = employeeInfo;
            instance.UserPath = Application.StartupPath + "\\Users\\" + employeeInfo.Name + "\\";
            if (!Directory.Exists(instance.UserPath)) Directory.CreateDirectory(instance.UserPath);
            instance.ReceiveFilePath = Application.StartupPath + "\\Recv\\";
            if (!Directory.Exists(instance.ReceiveFilePath)) Directory.CreateDirectory(instance.ReceiveFilePath);
            instance.TempPath = Application.StartupPath + "\\Temp\\";
            if (!Directory.Exists(instance.TempPath)) Directory.CreateDirectory(instance.TempPath);

            if (!File.Exists(instance.UserPath + "db.s3db")) File.Copy(Application.StartupPath + "\\db.s3db", instance.UserPath + "db.s3db");
            Db.MyWork = new Database(DatabaseType.Sqlite, "Data Source=" + instance.UserPath + "\\db.s3db;characterencoding=standard");
            //if (!File.Exists(instance.UserPath + "db.mdb")) File.Copy(Application.StartupPath + "\\db.mdb", instance.UserPath + "db.mdb");
            //Db.MyWork = new Database(DatabaseType.MsAccess, "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + instance.UserPath + "\\db.mdb");
        }

        /// <summary>
        /// 尝试加载自动登录的用户信息，不存在返回null
        /// </summary>
        /// <returns></returns>
        public static EmployeeInfo Load()
        {
            if (File.Exists(AutoLoginInfoPath))
            {
                string[] info = AES.Decode(File.ReadAllText(AutoLoginInfoPath), LOCALPASSWORDKEY).Split('\n');
                if (info.Length == 2 && !string.IsNullOrEmpty(info[0]) && !string.IsNullOrEmpty(info[1]))
                {
                    EmployeeInfo em = new EmployeeInfo();
                    em.UserName = info[0];
                    em.PassWord = info[1];
                    return em;
                }
            }
            return null;
        }

        /// <summary>
        /// 保存当前用户数据为自动登录
        /// </summary>
        public void Save()
        {
            File.WriteAllText(AutoLoginInfoPath, AES.Encode(EmployeeInfo.UserName + "\n" + EmployeeInfo.PassWord, LOCALPASSWORDKEY));
        }

        private UserSession() { }

        /// <summary>
        /// 用户个人文件夹路径
        /// </summary>
        public string UserPath { get; set; }
        public string ReceiveFilePath { get; set; }
        public string TempPath { get; set; }
        public EmployeeInfo EmployeeInfo { get; set; }
    }
}
